package com.dbh.jdk.concurrent;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *  倒计时器示例
 *
 * @author dongbanghui
 * @date 2018/06/02
 */
public class CountDownLatchDemo implements Runnable {

	/**
	 * 表示技术数量为10
	 */
	static final CountDownLatch end = new CountDownLatch(10);
	static final CountDownLatchDemo demo = new CountDownLatchDemo();

	@Override
	public void run() {
		try {
			Thread.sleep(new Random().nextInt(10)*1000);
			System.out.println("check complete");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally {
			//倒计时器减1
			end.countDown();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		ExecutorService exec = Executors.newFixedThreadPool(10);
		for (int i=0;i<10;i++){
			exec.submit(demo);
		}
		//等待检查
		end.await();
		//发射火箭
		System.out.println("Fire!");
		exec.shutdown();
	}

}
